package org.yoki.edu.portal.core.trace.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yunzhitx.cloud.common.model.PageResult;
import com.yunzhitx.cloud.common.model.ddd.AbstractEntity;
import com.yunzhitx.cloud.common.model.ddd.InstanceFactory;
import com.yunzhitx.cloud.common.model.exception.Shift;
import lombok.Data;

import java.util.Date;

import org.yoki.edu.portal.core.exception.BasicRestStatus;
import org.yoki.edu.portal.core.trace.infra.TraceThreePointRepository;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Column;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.List;

/**
 * 代码生成器自动生成
 * Date:2018-2-6 11:06:15
 *
 * @author
 */
@Data
@Table(name = "tra_trace_three_point")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TraceThreePoint extends AbstractEntity<TraceThreePointRepository> implements Serializable {

    private static final long serialVersionUID = 1L;

    /** */
    @Column(name = "fd_userId")
    private Integer userId;
    /** */
    @Column(name = "fd_userMac")
    private String userMac;
    /**
     * 年月日，如：20180101
     */
    @Column(name = "fd_date")
    private Integer date;
    /**
     * 时分秒，如：090101
     */
    @Column(name = "fd_time")
    private Integer time;
    /** */
    @Column(name = "fd_maxRssi")
    private Byte maxRssi;
    /** */
    @Column(name = "fd_maxRssiApMac")
    private String maxRssiApMac;
    /** */
    @Column(name = "fd_middleRssi")
    private Byte middleRssi;
    /** */
    @Column(name = "fd_middleRssiApMac")
    private String middleRssiApMac;
    /** */
    @Column(name = "fd_minRssi")
    private Byte minRssi;
    /** */
    @Column(name = "fd_minRssiApMac")
    private String minRssiApMac;
    /**
     * 备注
     */
    @Column(name = "fd_remark")
    private String remark;

    public TraceThreePoint() {
    }

    public TraceThreePoint(Integer id) {
        setId(id);
    }

    @Override
    public Serializable getKey() {
        return getId();
    }

    @Override
    public boolean existed() {
        return false;
    }

    private static TraceThreePointRepository traceThreePointRepository;

    public static TraceThreePointRepository getTraceThreePointRepository() {
        if (null == traceThreePointRepository) {
            traceThreePointRepository = InstanceFactory.getInstance(TraceThreePointRepository.class);
        }
        return traceThreePointRepository;
    }

    /* ************************************************************************************ *
       ***************************************  增  ***************************************
    * ************************************************************************************* */

    public void save() {
        /*
        if (existed()) {
            Shift.fatal(BasicRestStatus.repeatDataErrorStatus);
        }
        */
        getTraceThreePointRepository().insert(this);
    }

    /* ************************************************************************************ *
       ***************************************  删  ***************************************
    * ************************************************************************************* */

    /**
     * 根据ID删除组件<br>
     * 同时删除所有的组件参数、组件内元件参数
     *
     * @param id
     */
    public static void deleteById(Integer id) {
        if (null == id) {
            Shift.fatal(BasicRestStatus.paramNullErrorStatus);
        }
        getTraceThreePointRepository().deleteByPrimaryKey(id);
    }

    /* ************************************************************************************ *
       ***************************************  改  ***************************************
    * ************************************************************************************* */

    /**
     * 根据Id，更新TraceThreePoint
     *
     * @param
     * @return
     */
    public void update() {
        getTraceThreePointRepository().updateByPrimaryKeySelective(this);
    }

    public void replaceWeakSignal(final byte rssi, final String apMac) {
        if (null == apMac || apMac.length() <= 0
                || (null != minRssi && Math.abs(rssi) < Math.abs(minRssi))) {
            return;
        }
        if (apMac.equals(maxRssiApMac)) {
            maxRssi = rssi;
        } else if (apMac.equals(middleRssiApMac)) {
            middleRssi = rssi;
        } else if (apMac.equals(minRssiApMac)) {
            minRssi = rssi;
        } else if (null == minRssi || rssi > minRssi) {
            minRssi = rssi;
            minRssiApMac = apMac;
        }
        //排序
        if (null != minRssi) {
            if (null == middleRssi || (null != middleRssi && minRssi > middleRssi)) {
                Byte temp = middleRssi;
                middleRssi = minRssi;
                minRssi = temp;
                String tempMac = middleRssiApMac ;
                middleRssiApMac = minRssiApMac ;
                minRssiApMac = tempMac ;
            }
        }
        if (null != middleRssi) {
            if (null == maxRssi || (null != maxRssi && middleRssi > maxRssi)) {
                Byte temp = maxRssi;
                maxRssi = middleRssi;
                middleRssi = temp;
                String tempMac = maxRssiApMac ;
                maxRssiApMac = middleRssiApMac ;
                middleRssiApMac = tempMac ;
            }
        }
    }

    /* ************************************************************************************ *
       ***************************************  查  ***************************************
    * ************************************************************************************* */

    /**
     * 根据主键ID查询TraceThreePoint
     *
     * @return
     */
    /*
    public static TraceThreePoint selectDetailById(int id) {
        TraceThreePoint entity = getTraceThreePointRepository().selectByPrimaryKey(id);
        return entity;
    }
    */
    public static List<TraceThreePoint> selectEffectiveList(Integer startDate, Integer startTime, Integer endDate, Integer endTime) {
        Example e = new Example(TraceThreePoint.class);
        Example.Criteria criteria = e.createCriteria();
        criteria.andIsNotNull("minRssiApMac");
        if (null != startDate && null != startTime) {
            criteria.andGreaterThanOrEqualTo("date", startDate).andGreaterThanOrEqualTo("time", startTime);
        }
        if (null != endDate && null != endTime) {
            criteria.andLessThanOrEqualTo("date", endDate).andLessThanOrEqualTo("time", endTime);
        }
        List<TraceThreePoint> list = getTraceThreePointRepository().selectByExample(e);
        return list;
    }

    /**
     * 根据查询条件查询分页列表
     *
     * @param page
     * @return
     */

    public PageResult<TraceThreePoint> selectPageByQuery(PageResult page) {
    /*
        TraceThreePointPageQueryDTO dto = new TraceThreePointPageQueryDTO();
        // TODO 设置dto参数
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        PageInfo<TraceThreePoint> pageInfo = new PageInfo<>(getTraceThreePointRepository().selectPageByQuery(dto));
        page.setTotal(pageInfo.getTotal());
        page.setPageData(pageInfo.getList());
        return page;
    */
        return null;
    }


}